<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <meta name="renderer" content="webkit">

  
  <title>Android 14 NotificationChannels与Notification的加载流程 | 龙之叶的博客</title>

  <link rel="shortcut icon" href="/images/favicon.png">
  <link rel="alternate" href="/atom.xml" title="龙之叶的博客" type="application/atom+xml">
  <meta name="description" content="Android 14 NotificationChannels与Notification的加载流程">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 14 NotificationChannels与Notification的加载流程">
<meta property="og:url" content="http://longzhiye.top/2024/01/27/2024-01-27/index.html">
<meta property="og:site_name" content="龙之叶的博客">
<meta property="og:description" content="Android 14 NotificationChannels与Notification的加载流程">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://longzhiye.top/images/20240127/20240127001.png">
<meta property="og:image" content="http://longzhiye.top/images/20240127/20240127002.png">
<meta property="og:image" content="http://longzhiye.top/images/20240127/20240127003.png">
<meta property="og:image" content="http://longzhiye.top/images/20240127/20240127004.png">
<meta property="article:published_time" content="2024-01-26T16:00:00.000Z">
<meta property="article:modified_time" content="2024-08-29T02:40:34.920Z">
<meta property="article:author" content="龙之叶">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="framework">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://longzhiye.top/images/20240127/20240127001.png">

  <meta name="keywords" content=",Android,framework">
  <meta name="format-detection" content="telephone=no,email=no">
  <meta name="theme-color" content="#9C27B0">

  <meta name="mobile-web-app-capable" content="yes">
  <meta name="application-name" content="龙之叶的博客">
  <meta name="msapplication-starturl" content="http://longzhiye.top/2024/01/27/2024-01-27/">
  <meta name="msapplication-navbutton-color" content="#9C27B0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-title" content="龙之叶的博客">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/images/favicon.png">

  
    <link rel="canonical" href="http://longzhiye.top/2024/01/27/2024-01-27/">
  

  
  

  
  
  

  
<link rel="stylesheet" href="/css/mdui.css">
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/custom.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-pink">
  <script>var a=localStorage.getItem("mdui-theme-layout-dark");if(a){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>var a=localStorage.getItem("mdui-drawer-close");if(!a){document.getElementsByTagName("body")[0].className+=" mdui-drawer-body-left"};</script>
  <header id="header" class="mdui-appbar mdui-appbar-fixed mdui-appbar-scroll-hide mdui-appbar-inset">
  <div class="mdui-toolbar mdui-color-theme">
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-drawer="{target: '#sidebar', swipe: true}"><i class="mdui-icon material-icons">menu</i></a>
    <a href="/" class="mdui-typo-headline">龙之叶的博客</a>
    <div class="mdui-toolbar-spacer"></div>
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-dialog="{target: '#search'}" mdui-tooltip="{content: '搜索'}"><i class="mdui-icon material-icons">search</i></a>
	<!--
    <a href="/atom.xml" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'RSS'}"><i class="mdui-icon material-icons">rss_feed</i></a>
	-->
  </div>
</header>
<div class="mdui-dialog" id="search">
  
    <div class="search-form">
      <input type="search" class="search-form-input" placeholder="请输入关键字">
    </div>
    <div class="search-result" data-resource="/search.xml"></div>
  
</div>

  <aside id="sidebar" class="mdui-drawer mdui-drawer-full-height">
  <script>var a=localStorage.getItem("mdui-drawer-close");if(a){document.getElementById("sidebar").className+=" mdui-drawer-close"};</script>
  <div class="mdui-grid-tile">
    <img src="/images/banner.png" style="height: 160px;">
    <img src="/images/avatar.png" class="avatar-animation" style="position: absolute; top: 10%; left: 24px; width: 64px; height: 64px; border: 2px solid #fff; border-radius: 50%;background-color:#fff;">
    <div class="mdui-grid-tile-actions">
      <div class="mdui-grid-tile-text">
        <div class="mdui-grid-tile-title">龙之叶</div>
        <div class="mdui-grid-tile-subtitle"><i class="mdui-icon material-icons">art_track</i>生命不息，折腾不止</div>
      </div>
      
        <div class="mdui-grid-tile-buttons">
          <a href="mailto:longzhiye163@163.com" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'longzhiye163@163.com'}"><i class="mdui-icon material-icons">email</i></a>
        </div>
      
    </div>
  </div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    <a href="/" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-blue">home</i>
      <div class="mdui-list-item-content">主页</div>
    </a>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-0");if(a){document.getElementsByClassName("mdui-collapse-item")[0].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-deep-orange">archive</i>
        <div class="mdui-list-item-content">归档</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/archives/2025/07/">七月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/06/">六月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/03/">三月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/02/">二月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/01/">一月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/10/">十月 2024<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/09/">九月 2024<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/08/">八月 2024<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/03/">三月 2024<span class="mdui-ripple sidebar_archives-count">5</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/02/">二月 2024<span class="mdui-ripple sidebar_archives-count">11</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/01/">一月 2024<span class="mdui-ripple sidebar_archives-count">9</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/12/">十二月 2023<span class="mdui-ripple sidebar_archives-count">10</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/11/">十一月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/10/">十月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/09/">九月 2023<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/08/">八月 2023<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/06/">六月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/05/">五月 2023<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/04/">四月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/03/">三月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/02/">二月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/09/">九月 2022<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/08/">八月 2022<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/04/">四月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/03/">三月 2017<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/02/">二月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/01/">一月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/12/">十二月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/11/">十一月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/10/">十月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/08/">八月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-1");if(a){document.getElementsByClassName("mdui-collapse-item")[1].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-green">class</i>
        <div class="mdui-list-item-content">分类</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术<span class="mdui-ripple sidebar_archives-count">98</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E7%94%9F%E6%B4%BB/">生活<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E9%80%9A%E7%9F%A5/">通知<span class="mdui-ripple sidebar_archives-count">4</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-2");if(a){document.getElementsByClassName("mdui-collapse-item")[2].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-brown">bookmark</i>
        <div class="mdui-list-item-content">标签</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/tags/ANR/" rel="tag">ANR<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/APP/" rel="tag">APP<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android/" rel="tag">Android<span class="mdui-ripple sidebar_archives-count">89</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android12/" rel="tag">Android12<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android13/" rel="tag">Android13<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android14/" rel="tag">Android14<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AndroidStudio/" rel="tag">AndroidStudio<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AutoDraw/" rel="tag">AutoDraw<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Crash/" rel="tag">Crash<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Foxmail/" rel="tag">Foxmail<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Framework/" rel="tag">Framework<span class="mdui-ripple sidebar_archives-count">20</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GPS%E8%BD%A8%E8%BF%B9%E5%9B%BE/" rel="tag">GPS轨迹图<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Git/" rel="tag">Git<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GitHub/" rel="tag">GitHub<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Google/" rel="tag">Google<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Hexo/" rel="tag">Hexo<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Java/" rel="tag">Java<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Kernel/" rel="tag">Kernel<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Launcher/" rel="tag">Launcher<span class="mdui-ripple sidebar_archives-count">17</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Lib/" rel="tag">Lib<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Linux/" rel="tag">Linux<span class="mdui-ripple sidebar_archives-count">18</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Markdown/" rel="tag">Markdown<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Material-Design/" rel="tag">Material Design<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OS-X/" rel="tag">OS X<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OTA/" rel="tag">OTA<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/QuickTime/" rel="tag">QuickTime<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SSH/" rel="tag">SSH<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Settings/" rel="tag">Settings<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SystemUI/" rel="tag">SystemUI<span class="mdui-ripple sidebar_archives-count">19</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Ubuntu/" rel="tag">Ubuntu<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WPS/" rel="tag">WPS<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Wifi/" rel="tag">Wifi<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WindTerm/" rel="tag">WindTerm<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Windows/" rel="tag">Windows<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/androidmk/" rel="tag">androidmk<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/blog/" rel="tag">blog<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/build/" rel="tag">build<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/feel/" rel="tag">feel<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/framework/" rel="tag">framework<span class="mdui-ripple sidebar_archives-count">50</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/git/" rel="tag">git<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/inotify/" rel="tag">inotify<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/life/" rel="tag">life<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/ninja/" rel="tag">ninja<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/rsync/" rel="tag">rsync<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/standard/" rel="tag">standard<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/test/" rel="tag">test<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1/" rel="tag">企业微信<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E7%94%9F%E6%B4%BB%E6%84%9F%E6%82%9F/" rel="tag">生活感悟<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <a href="/about" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-purple">person</i>
      <div class="mdui-list-item-content">关于</div>
    </a>
  </div>

  <div class="mdui-divider"></div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    
      <a href="/timeline" class="mdui-list-item mdui-ripple">时间轴</a>
    
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-3");if(a){document.getElementsByClassName("mdui-collapse-item")[3].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <div class="mdui-list-item-content">友情链接</div>
        <i class="mdui-list-item-icon mdui-icon material-icons">link</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
          <a href="http://www.niemingzhao.top" target="_blank" class="mdui-list-item mdui-p-l-2 mdui-text-color-theme-accent mdui-ripple" style="justify-content: center;">聂明照的博客</a>
        
        
      </div>
    </div>
  </div>
</aside>

  <main id="main" class="mdui-m-t-5 fadeIn animated">
  
<link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  <style>#main article .mdui-card-content .center-block{display:block!important;margin-right:auto!important;margin-left:auto!important}</style>
  <style>#main article .mdui-card-content .text-center{text-align:center!important}</style>
  <article class="mdui-card mdui-m-b-5">
    <header class="mdui-card-media">
      <img src="/images/random/material-5.png" style="max-height: 240px;">
      <div class="mdui-card-media-covered">
        <div class="mdui-card-primary">
          <div class="mdui-card-primary-title">Android 14 NotificationChannels与Notification的加载流程</div>
          <div class="mdui-card-primary-subtitle"><i class="iconfont">&#xe697;</i> 2024-01-27 / <i class="iconfont">&#xe601;</i> 龙之叶 &nbsp;&nbsp; <span id="busuanzi_container_page_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_page_pv"></span></span></div>
        </div>
      </div>
      <div class="mdui-card-menu">
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#qrcode', align: 'right'}"><i class="mdui-icon material-icons">devices</i></button>
          <ul class="mdui-menu" id="qrcode">
            
              <li class="mdui-menu-item"><a class="mdui-text-center mdui-ripple">发送到手机</a></li>
            
            <li class="mdui-menu-item" disabled>
              
                <img src="//qr.liantu.com/api.php?w=246&m=10&text=http://longzhiye.top/2024/01/27/2024-01-27/">
              
            </li>
          </ul>
        
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#share_menu', align: 'right'}"><i class="mdui-icon material-icons">share</i></button>
          <ul class="mdui-menu" id="share_menu">
            <li class="mdui-menu-item">
              <a href="https://service.weibo.com/share/share.php?appkey=&title=Android 14 NotificationChannels与Notification的加载流程&url=http://longzhiye.top/2024/01/27/2024-01-27/&pic=http://longzhiye.top/images/favicon.png&searchPic=false&style=simple" target="_blank" class="mdui-ripple">分享到 Weibo</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://twitter.com/intent/tweet?text=Android 14 NotificationChannels与Notification的加载流程&url=http://longzhiye.top/2024/01/27/2024-01-27/&via=龙之叶" target="_blank" class="mdui-ripple">分享到 Twitter</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.facebook.com/sharer/sharer.php?u=http://longzhiye.top/2024/01/27/2024-01-27/" target="_blank" class="mdui-ripple">分享到 Facebook</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://plus.google.com/share?url=http://longzhiye.top/2024/01/27/2024-01-27/" target="_blank" class="mdui-ripple">分享到 Google+</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.linkedin.com/shareArticle?mini=true&url=http://longzhiye.top/2024/01/27/2024-01-27/&title=Android 14 NotificationChannels与Notification的加载流程" target="_blank" class="mdui-ripple">分享到 LinkedIn</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://connect.qq.com/widget/shareqq/index.html?site=龙之叶的博客&title=Android 14 NotificationChannels与Notification的加载流程&pics=http://longzhiye.top/images/favicon.png&url=http://longzhiye.top/2024/01/27/2024-01-27/" target="_blank" class="mdui-ripple">分享到 QQ</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://telegram.me/share/url?url=http://longzhiye.top/2024/01/27/2024-01-27/&text=Android 14 NotificationChannels与Notification的加载流程" target="_blank" class="mdui-ripple">分享到 Telegram</a>
            </li>
          </ul>
        
      </div>
    </header>
    <div class="mdui-card-content mdui-typo">
      <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>这部分我觉得三方应用使用的较多，分析的时候也是源码与三方应用结合分析的。</p>
<h3 id="一-NotificationChannel-的创建"><a href="#一-NotificationChannel-的创建" class="headerlink" title="一. NotificationChannel 的创建"></a>一. NotificationChannel 的创建</h3><p>在源码中，我看到了一个很怪的类：NotificationChannels.java。这个类继承了 CoreStartable。</p>
<blockquote>
<p>注：CoreStartable 就是 SystemUI，只是我这的源码的命名不一样，下面为了便于他人阅读，就以 SystemUI 来叫。</p>
</blockquote>
<p>NotificationChannels.java 就百十行代码，很简单，一起看看这个类：<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/util/NotificationChannels.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">NotificationChannels</span> <span class="keyword">extends</span> <span class="title class_">CoreStartable</span> &#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="comment">// 省略代码</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">createAll</span><span class="params">(Context context)</span> &#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">NotificationManager</span> <span class="variable">nm</span> <span class="operator">=</span> context.getSystemService(NotificationManager.class);</span><br><span class="line">        <span class="comment">// 创建通道</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">NotificationChannel</span> <span class="variable">batteryChannel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">NotificationChannel</span>(BATTERY,</span><br><span class="line">                context.getString(R.string.notification_channel_battery),</span><br><span class="line">                NotificationManager.IMPORTANCE_MAX);</span><br><span class="line">        <span class="keyword">final</span> <span class="type">String</span> <span class="variable">soundPath</span> <span class="operator">=</span> Settings.Global.getString(context.getContentResolver(),</span><br><span class="line">                Settings.Global.LOW_BATTERY_SOUND);</span><br><span class="line">        batteryChannel.setSound(Uri.parse(<span class="string">&quot;file://&quot;</span> + soundPath), <span class="keyword">new</span> <span class="title class_">AudioAttributes</span>.Builder()</span><br><span class="line">                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)</span><br><span class="line">                .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)</span><br><span class="line">                .build());</span><br><span class="line">        batteryChannel.setBlockable(<span class="literal">true</span>);</span><br><span class="line">        <span class="comment">// 创建通道</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">NotificationChannel</span> <span class="variable">alerts</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">NotificationChannel</span>(</span><br><span class="line">                ALERTS,</span><br><span class="line">                context.getString(R.string.notification_channel_alerts),</span><br><span class="line">                NotificationManager.IMPORTANCE_HIGH);</span><br><span class="line">        <span class="comment">// 创建通道</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">NotificationChannel</span> <span class="variable">general</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">NotificationChannel</span>(</span><br><span class="line">                GENERAL,</span><br><span class="line">                context.getString(R.string.notification_channel_general),</span><br><span class="line">                NotificationManager.IMPORTANCE_MIN);</span><br><span class="line">        <span class="comment">// 创建通道</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">NotificationChannel</span> <span class="variable">storage</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">NotificationChannel</span>(</span><br><span class="line">                STORAGE,</span><br><span class="line">                context.getString(R.string.notification_channel_storage),</span><br><span class="line">                isTv(context)</span><br><span class="line">                        ? NotificationManager.IMPORTANCE_DEFAULT</span><br><span class="line">                        : NotificationManager.IMPORTANCE_LOW);</span><br><span class="line">        <span class="comment">// 创建通道</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">NotificationChannel</span> <span class="variable">hint</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">NotificationChannel</span>(</span><br><span class="line">                HINTS,</span><br><span class="line">                context.getString(R.string.notification_channel_hints),</span><br><span class="line">                NotificationManager.IMPORTANCE_DEFAULT);</span><br><span class="line">        <span class="comment">// No need to bypass DND.</span></span><br><span class="line">        <span class="comment">// 注册通道</span></span><br><span class="line">        nm.createNotificationChannels(Arrays.asList(</span><br><span class="line">                alerts,</span><br><span class="line">                general,</span><br><span class="line">                storage,</span><br><span class="line">                createScreenshotChannel(</span><br><span class="line">                        context.getString(R.string.notification_channel_screenshot),</span><br><span class="line">                        nm.getNotificationChannel(SCREENSHOTS_LEGACY)),</span><br><span class="line">                batteryChannel,</span><br><span class="line">                hint</span><br><span class="line">        ));</span><br><span class="line">        <span class="comment">// Delete older SS channel if present.</span></span><br><span class="line">        <span class="comment">// Screenshots promoted to heads-up in P, this cleans up the lower priority channel from O.</span></span><br><span class="line">        <span class="comment">// This line can be deleted in Q.</span></span><br><span class="line">        nm.deleteNotificationChannel(SCREENSHOTS_LEGACY);</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span> (isTv(context)) &#123;</span><br><span class="line">            <span class="comment">// TV specific notification channel for TV PIP controls.</span></span><br><span class="line">            <span class="comment">// Importance should be &#123;@link NotificationManager#IMPORTANCE_MAX&#125; to have the highest</span></span><br><span class="line">            <span class="comment">// priority, so it can be shown in all times.</span></span><br><span class="line">            <span class="comment">// 注册通道</span></span><br><span class="line">            nm.createNotificationChannel(<span class="keyword">new</span> <span class="title class_">NotificationChannel</span>(</span><br><span class="line">                    TVPIP,</span><br><span class="line">                    context.getString(R.string.notification_channel_tv_pip),</span><br><span class="line">                    NotificationManager.IMPORTANCE_MAX));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Set up screenshot channel, respecting any previously committed user settings on legacy</span></span><br><span class="line"><span class="comment">     * channel.</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@VisibleForTesting</span> <span class="keyword">static</span> NotificationChannel <span class="title function_">createScreenshotChannel</span><span class="params">(</span></span><br><span class="line"><span class="params">            String name, NotificationChannel legacySS)</span> &#123;</span><br><span class="line">        <span class="type">NotificationChannel</span> <span class="variable">screenshotChannel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">NotificationChannel</span>(SCREENSHOTS_HEADSUP,</span><br><span class="line">                name, NotificationManager.IMPORTANCE_HIGH); <span class="comment">// pop on screen</span></span><br><span class="line">        screenshotChannel.setSound(<span class="literal">null</span>, <span class="comment">// silent</span></span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">AudioAttributes</span>.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());</span><br><span class="line">        screenshotChannel.setBlockable(<span class="literal">true</span>);</span><br><span class="line">        <span class="keyword">if</span> (legacySS != <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// Respect any user modified fields from the old channel.</span></span><br><span class="line">            <span class="type">int</span> <span class="variable">userlock</span> <span class="operator">=</span> legacySS.getUserLockedFields();</span><br><span class="line">            <span class="keyword">if</span> ((userlock &amp; NotificationChannel.USER_LOCKED_IMPORTANCE) != <span class="number">0</span>) &#123;</span><br><span class="line">                screenshotChannel.setImportance(legacySS.getImportance());</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ((userlock &amp; NotificationChannel.USER_LOCKED_SOUND) != <span class="number">0</span>)  &#123;</span><br><span class="line">                screenshotChannel.setSound(legacySS.getSound(), legacySS.getAudioAttributes());</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ((userlock &amp; NotificationChannel.USER_LOCKED_VIBRATION) != <span class="number">0</span>)  &#123;</span><br><span class="line">               screenshotChannel.setVibrationPattern(legacySS.getVibrationPattern());</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ((userlock &amp; NotificationChannel.USER_LOCKED_LIGHTS) != <span class="number">0</span>)  &#123;</span><br><span class="line">                screenshotChannel.setLightColor(legacySS.getLightColor());</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// skip show_badge, irrelevant for system channel</span></span><br><span class="line">        &#125; </span><br><span class="line">        <span class="keyword">return</span> screenshotChannel;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">start</span><span class="params">()</span> &#123;</span><br><span class="line">        createAll(mContext);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">boolean</span> <span class="title function_">isTv</span><span class="params">(Context context)</span> &#123;</span><br><span class="line">        <span class="type">PackageManager</span> <span class="variable">packageManager</span> <span class="operator">=</span> context.getPackageManager();</span><br><span class="line">        <span class="keyword">return</span> packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>NotificationChannels 扩展自 SystemUI 并重写了 <strong>start()</strong> 方法，它执行了 <strong>createAll()</strong> 方法，创建了通知通道有 batteryChannel（电池）、alerts（提醒）、storage（存储空间）、screenshot（屏幕截图）、hint （提示）、general（常规消息）。<br>此外，如果是 TV 设备的话还会创建画中画通知通道。</p>
<blockquote>
<ul>
<li>怪在哪呢：为什么在这个类去创建注册那些通知通道，而且并没有提示消息什么的，意义在哪？</li>
<li>注：下面我把该类当作三方应用。</li>
</ul>
</blockquote>
<p>下面围绕 NotificationChannels 一步一步的分析，上面调用 <strong>new NotificationChannel()</strong> 创建通知通道，然后 调用 <strong>nm.createNotificationChannels()</strong> 方法注册通道。<br>nm 其实是 NotificationManager 的对象，这样就转到了 NotificationManager 中。这里我作了一个流程图：<br><img src="/images/20240127/20240127001.png"></p>
<p>从 NotificationManager.createNotificationChannel() 到 NotificationManagerService.createNotificationChannelsImpl() 都是正常流程，也好理解。创建的关键代码在 <strong>mPreferencesHelper.createNotificationChannel()</strong> 中，具体如下：<br><strong>frameworks/base/services/core/java/com/android/server/notification/PreferencesHelper.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">createNotificationChannel</span><span class="params">(String pkg, <span class="type">int</span> uid, NotificationChannel channel,</span></span><br><span class="line"><span class="params">        <span class="type">boolean</span> fromTargetApp, <span class="type">boolean</span> hasDndAccess)</span> &#123;</span><br><span class="line">    Objects.requireNonNull(pkg);</span><br><span class="line">    Objects.requireNonNull(channel);</span><br><span class="line">    Objects.requireNonNull(channel.getId());</span><br><span class="line">    Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName()));</span><br><span class="line">    <span class="type">boolean</span> <span class="variable">needsPolicyFileChange</span> <span class="operator">=</span> <span class="literal">false</span>, wasUndeleted = <span class="literal">false</span>, needsDndChange = <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">synchronized</span> (mPackagePreferences) &#123;</span><br><span class="line">        <span class="type">PackagePreferences</span> <span class="variable">r</span> <span class="operator">=</span> getOrCreatePackagePreferencesLocked(pkg, uid);</span><br><span class="line">        <span class="keyword">if</span> (r == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>(<span class="string">&quot;Invalid package&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (channel.getGroup() != <span class="literal">null</span> &amp;&amp; !r.groups.containsKey(channel.getGroup())) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>(<span class="string">&quot;NotificationChannelGroup doesn&#x27;t exist&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel.getId())) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>(<span class="string">&quot;Reserved id&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 前面是各种条件检查，下面这行是关键点，先检索这个 channel 是否已经存在，以 channel id 为标志位。</span></span><br><span class="line">        <span class="type">NotificationChannel</span> <span class="variable">existing</span> <span class="operator">=</span> r.channels.get(channel.getId());</span><br><span class="line">        <span class="comment">// 如果通道已经存在就更新通道</span></span><br><span class="line">        <span class="comment">//  更新通道保留大部分已存在的设置，只更新了 name，description 等几项</span></span><br><span class="line">        <span class="keyword">if</span> (existing != <span class="literal">null</span> &amp;&amp; fromTargetApp) &#123;</span><br><span class="line">             <span class="comment">// 省略部分代码......</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">             <span class="comment">// 省略部分代码......</span></span><br><span class="line">            <span class="comment">// channel 未创建过，把用户创建的 channel 加入到系统的 cache 里</span></span><br><span class="line">            r.channels.put(channel.getId(), channel);</span><br><span class="line">            <span class="keyword">if</span> (channel.canBypassDnd() != mAreChannelsBypassingDnd) &#123;</span><br><span class="line">                needsDndChange = <span class="literal">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            MetricsLogger.action(getChannelLog(channel, pkg).setType(</span><br><span class="line">                    com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_OPEN));</span><br><span class="line">            mNotificationChannelLogger.logNotificationChannelCreated(channel, uid, pkg);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (needsDndChange) &#123;</span><br><span class="line">        updateChannelsBypassingDnd();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> needsPolicyFileChange;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>至此，一个通知完整的创建完成。<br>其实通过mPreferencesHelper.createNotificationChannel() 方法还能看出 NotificationChannel 一旦创建，那么能更改的东西就很少了（只有名字，描述，blocksystem，以及优先级），而 blocksystem 属性只有在系统源码里面才能使用（hide）；</p>
<p>NotificationChannel 不会重复创建。</p>
<blockquote>
<p>Android官方是这么解释这个设计的：NotificationChannel 就像是开发者送给用户的一个精美礼物，一旦送出去，控制权就在用户那里了。即使用户把通知铃声设置成《江南style》，你可以知道，但不可以更改。</p>
</blockquote>
<h3 id="二-Notification-的显示过程"><a href="#二-Notification-的显示过程" class="headerlink" title="二. Notification 的显示过程"></a>二. Notification 的显示过程</h3><p>这里代码有点多，我制作了一个通知传递的方法调用流程图：</p>
<p><img src="/images/20240127/20240127002.png"></p>
<p>上述流程图中，我们可能更比较关注 NotificationManagerService 是怎么与 SystemUI 交互的。<br>其实SystemUI向 NotificationManagerService 注册一个”服务”(一个Binder)。这个”服务”就相当于客户端 SystemUI 在服务端 NotificationManagerService 注册的一个回调。当有通知来临的时候，就会通过这个”服务”通知SystemUI，这个注册是在 <strong>StatusBar#setUpPresenter()</strong> 中完成的：<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">setUpPresenter</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="comment">// 省略部分代码......  </span></span><br><span class="line">    <span class="comment">// 这位置调用了NotificationsControllerImpl#initialize()的方法</span></span><br><span class="line">    mNotificationsController.initialize(</span><br><span class="line">            mPresenter,</span><br><span class="line">            mNotifListContainer,</span><br><span class="line">            mStackScrollerController.getNotifStackController(),</span><br><span class="line">            mNotificationActivityStarter,</span><br><span class="line">            mCentralSurfacesComponent.getBindRowCallback());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在 <strong>NotificationsControllerImpl#initialize()</strong> 中进行注册：<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/notification/init/NotificationsControllerImpl.kt</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">override fun <span class="title function_">initialize</span><span class="params">(</span></span><br><span class="line"><span class="params">    presenter: NotificationPresenter,</span></span><br><span class="line"><span class="params">    listContainer: NotificationListContainer,</span></span><br><span class="line"><span class="params">    stackController: NotifStackController,</span></span><br><span class="line"><span class="params">    notificationActivityStarter: NotificationActivityStarter,</span></span><br><span class="line"><span class="params">    bindRowCallback: NotificationRowBinderImpl.BindRowCallback</span></span><br><span class="line"><span class="params">)</span> &#123;</span><br><span class="line">    <span class="comment">// 注册回调</span></span><br><span class="line">    notificationListener.registerAsSystemService()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述注册了之后，每当有通知来时就会回调到：<strong>NotificationListener#onNotificationPosted()</strong> 中，接着就会到 NotificationEntryManager中。<br>下面分析通知视图的加载，这里就直接从 <strong>NotificationEntryManager#onNotificationPosted()</strong> 开始。<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coalescer/GroupCoalescer.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">final</span> <span class="type">NotificationHandler</span> <span class="variable">mNotifListener</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">NotificationHandler</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onNotificationPosted</span><span class="params">(StatusBarNotification sbn, RankingMap rankingMap)</span> &#123;</span><br><span class="line">        maybeEmitBatch(sbn);</span><br><span class="line">        applyRanking(rankingMap);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">shouldCoalesce</span> <span class="operator">=</span> handleNotificationPosted(sbn, rankingMap);</span><br><span class="line">        <span class="comment">// 通过key值进行判断，通知是否已存在</span></span><br><span class="line">        <span class="keyword">if</span> (shouldCoalesce) &#123;</span><br><span class="line">            mLogger.logEventCoalesced(sbn.getKey());</span><br><span class="line">            mHandler.onNotificationRankingUpdate(rankingMap);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            mHandler.onNotificationPosted(sbn, rankingMap);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>通过上述源码可以知道，通知到后，首先会进行判断该通知是否存在，存在则刷新，不存在则添加；这里以添加为例去分析。</p>
<p>先看两张图，可以知道下面分析的方向：</p>
<p><strong>SystemUI组件思维导图</strong>:<br><img src="/images/20240127/20240127003.png"><br><strong>SystemUI 关键布局图</strong><br><img src="/images/20240127/20240127004.png"></p>
<p><strong>根布局</strong>：super_status_bar.xml,<br><strong>顶上状态栏</strong>： status_bar.xml, 通过CollapsedStatusBarFragment.java加载；PhoneStatusBarView(FrameLayout,)是里面的父控件; 对应 R.id.status_bar_container 。<br><strong>下拉状态栏</strong>：（包括通知为status_bar_expanded.xml）,最外层布局NotificationPanelView；qs_frame.xml 为下拉后的状态栏部分(用QSFragment管理，布局控件为QSContainerImpl)，其高度更新在QSContainerImpl.java中；<br><strong>NotificationStackScrollLayout</strong>: 用于下拉的通知的相关问题(占满全屏，包括导航栏，会处理点击状态栏空白区的逻辑)。</p>
<blockquote>
<p>NotificationStackScrollLayout：是一个滑动布局，里面嵌套着 ExpandableNotificationRow ，即通知。</p>
</blockquote>
<p>接着上面分析：上面我们只关注 addNotification(sbn, rankingMap) ，而它内部时调用 addNotificationInternal() 方法实现的。<br><strong>NotificationEntryManager#addNotificationInternal()</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// NotificationEntryManager.java</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">addNotificationInternal</span><span class="params">(</span></span><br><span class="line"><span class="params">        StatusBarNotification notification,</span></span><br><span class="line"><span class="params">        RankingMap rankingMap)</span> <span class="keyword">throws</span> InflationException &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 省略部分代码 ...</span></span><br><span class="line">    <span class="type">NotificationEntry</span> <span class="variable">entry</span> <span class="operator">=</span> mPendingNotifications.get(key);</span><br><span class="line">    <span class="comment">// 省略部分代码 ...</span></span><br><span class="line">    <span class="comment">// 构造视图</span></span><br><span class="line">    <span class="keyword">if</span> (!mFeatureFlags.isNewNotifPipelineRenderingEnabled()) &#123;</span><br><span class="line">        <span class="comment">// NotificationRowBinderImpl 为 NotificationRowBinder 的实现类</span></span><br><span class="line">        mNotificationRowBinderLazy.get().inflateViews(entry, <span class="literal">null</span>, mInflationCallback);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 省略部分代码 ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>首先为通知创建一个 NotificationEntry 通知实例，然后再通过 NotificationRowBinderImpl 中的 inflateViews() 加载通知视图，绑定通知信息，并在通知栏添加通知视图，以及在状态栏添加通知图标。<br><strong>NotificationRowBinderImpl#inflateViews()</strong><br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotificationRowBinderImpl.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">inflateViews</span><span class="params">(</span></span><br><span class="line"><span class="params">        NotificationEntry entry,</span></span><br><span class="line"><span class="params">        NotifInflater.Params params,</span></span><br><span class="line"><span class="params">        NotificationRowContentBinder.InflationCallback callback)</span></span><br><span class="line">        <span class="keyword">throws</span> InflationException &#123;</span><br><span class="line">    <span class="keyword">if</span> (params == <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="comment">// weak assert that the params should always be passed in the new pipeline</span></span><br><span class="line">        mNotifPipelineFlags.checkLegacyPipelineEnabled();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 获取查看父布局</span></span><br><span class="line">    <span class="type">ViewGroup</span> <span class="variable">parent</span> <span class="operator">=</span> mListContainer.getViewParentForNotification(entry);</span><br><span class="line">    <span class="comment">// 通知是否存在</span></span><br><span class="line">    <span class="keyword">if</span> (entry.rowExists()) &#123;</span><br><span class="line">        mIconManager.updateIcons(entry);</span><br><span class="line">        <span class="type">ExpandableNotificationRow</span> <span class="variable">row</span> <span class="operator">=</span> entry.getRow();</span><br><span class="line">        row.reset();</span><br><span class="line">        updateRow(entry, row);</span><br><span class="line">        inflateContentViews(entry, params, row, callback);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 创建图标</span></span><br><span class="line">        mIconManager.createIcons(entry);</span><br><span class="line">        mRowInflaterTaskProvider.get().inflate(mContext, parent, entry,</span><br><span class="line">                row -&gt; &#123;</span><br><span class="line">                    <span class="comment">// 为视图设置控制器.</span></span><br><span class="line">                    <span class="type">ExpandableNotificationRowComponent</span> <span class="variable">component</span> <span class="operator">=</span></span><br><span class="line">                            mExpandableNotificationRowComponentBuilder</span><br><span class="line">                                    .expandableNotificationRow(row)</span><br><span class="line">                                    .notificationEntry(entry)</span><br><span class="line">                                    .onExpandClickListener(mPresenter)</span><br><span class="line">                                    .listContainer(mListContainer)</span><br><span class="line">                                    .build();</span><br><span class="line">                    <span class="type">ExpandableNotificationRowController</span> <span class="variable">rowController</span> <span class="operator">=</span></span><br><span class="line">                            component.getExpandableNotificationRowController();</span><br><span class="line">                    rowController.init(entry);</span><br><span class="line">                    entry.setRowController(rowController);</span><br><span class="line">                    bindRow(entry, row);</span><br><span class="line">                    updateRow(entry, row);</span><br><span class="line">                    inflateContentViews(entry, params, row, callback);</span><br><span class="line">                &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面无论走哪个分支，最后进入到inflateContentViews(entry, row, callback);这是一个回调：<br><strong>NotificationRowBinderImpl#inflateContentViews()</strong><br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotificationRowBinderImpl.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 加载该行的基本内容视图</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">inflateContentViews</span><span class="params">(</span></span><br><span class="line"><span class="params">        NotificationEntry entry,</span></span><br><span class="line"><span class="params">        NotifInflater.Params inflaterParams,</span></span><br><span class="line"><span class="params">        ExpandableNotificationRow row,</span></span><br><span class="line"><span class="params">        <span class="meta">@Nullable</span> NotificationRowContentBinder.InflationCallback inflationCallback)</span> &#123;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">// 省略部分代码......</span></span><br><span class="line">    params.rebindAllContentViews();</span><br><span class="line">    mRowContentBindStage.requestRebind(entry, en -&gt; &#123;</span><br><span class="line">        row.setUsesIncreasedCollapsedHeight(useIncreasedCollapsedHeight);</span><br><span class="line">        row.setIsLowPriority(isLowPriority);</span><br><span class="line">        <span class="keyword">if</span> (inflationCallback != <span class="literal">null</span>) &#123;</span><br><span class="line">            inflationCallback.onAsyncInflationFinished(en);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>inflationCallback 是 NotificationRowContentBinder 的一个内部接口；在 NotificationEntryManager 中被实现，所以将回调到 <strong>NotificationEntryManager#onAsyncInflationFinished()</strong> 中。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// NotificationEntryManager.java</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAsyncInflationFinished</span><span class="params">(NotificationEntry entry)</span> &#123;</span><br><span class="line">    Trace.beginSection(<span class="string">&quot;NotificationEntryManager.onAsyncInflationFinished&quot;</span>);</span><br><span class="line">    mPendingNotifications.remove(entry.getKey());</span><br><span class="line">    <span class="comment">// If there was an async task started after the removal, we don&#x27;t want to add it back to</span></span><br><span class="line">    <span class="comment">// the list, otherwise we might get leaks.</span></span><br><span class="line">    <span class="keyword">if</span> (!entry.isRowRemoved()) &#123;</span><br><span class="line">        <span class="type">boolean</span> <span class="variable">isNew</span> <span class="operator">=</span> getActiveNotificationUnfiltered(entry.getKey()) == <span class="literal">null</span>;</span><br><span class="line">            <span class="comment">// 省略部分代码......</span></span><br><span class="line">        <span class="keyword">if</span> (isNew) &#123;</span><br><span class="line">            <span class="comment">// 省略部分代码......</span></span><br><span class="line">            <span class="comment">// 添加一个notification会走到这里、</span></span><br><span class="line">            <span class="comment">// 包括一开机就显示出来的那些notification</span></span><br><span class="line">            addActiveNotification(entry);</span><br><span class="line">            <span class="comment">// 更新视图</span></span><br><span class="line">            updateNotifications(<span class="string">&quot;onAsyncInflationFinished&quot;</span>);</span><br><span class="line">            <span class="comment">// 省略部分代码......</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 省略部分代码......</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    Trace.endSection();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里直接看 updateNotifications(“onAsyncInflationFinished”) 方法；<br><strong>NotificationEntryManager#updateNotification()</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// NotificationEntryManager.java</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updateNotifications</span><span class="params">(String reason)</span> &#123;</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">    <span class="keyword">if</span> (mPresenter != <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="comment">// 更新视图</span></span><br><span class="line">        mPresenter.updateNotificationViews(reason);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>mPresenter 的实现类是 StatusBarNotificationPresenter，所以接着看其里面的 updateNotificationViews() 方法。<br><strong>StatusBarNotificationPresenter#updateNotificationViews()</strong><br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationPresenter.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updateNotificationViews</span><span class="params">(<span class="keyword">final</span> String reason)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (!mNotifPipelineFlags.checkLegacyPipelineEnabled()) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// The function updateRowStates depends on both of these being non-null, so check them here.</span></span><br><span class="line">    <span class="comment">// We may be called before they are set from DeviceProvisionedController&#x27;s callback.</span></span><br><span class="line">    <span class="keyword">if</span> (mScrimController == <span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">    <span class="comment">// 不要在折叠期间修改通知。.</span></span><br><span class="line">    <span class="keyword">if</span> (isCollapsing()) &#123;</span><br><span class="line">        mShadeController.addPostCollapseAction(() -&gt; updateNotificationViews(reason));</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 把通知视图添加到通知面版的通知栏中</span></span><br><span class="line">    mViewHierarchyManager.updateNotificationViews();</span><br><span class="line">    <span class="comment">// 这里不仅仅更新了通知面版的通知视图，也更新了状态栏的通知图标</span></span><br><span class="line">    mNotificationPanel.updateNotificationViews(reason);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们这里看通知面板更新，即 mNotificationPanel.updateNotificationViews(reason) 方法。mNotificationPanel 为 NotificationPanelViewController 的对象。<br><strong>NotificationPanelViewController#updateNotificationViews(reason)</strong><br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/shade/NotificationPanelViewController.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 更新通知视图的部分和状态栏图标。每当显示的基础通知数据发生更改时，</span></span><br><span class="line"><span class="comment">// 这由 NotificationPresenter 触发。</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updateNotificationViews</span><span class="params">(String reason)</span> &#123;</span><br><span class="line">    <span class="comment">// 更新NotificationStackScrollLayout 这个视图类的各种信息</span></span><br><span class="line">    <span class="comment">// updateSectionBoundaries() 这个方法还没弄明白，但我估计是添加/删除视图后布局重新定位，以及一个</span></span><br><span class="line">    mNotificationStackScrollLayoutController.updateSectionBoundaries(reason);</span><br><span class="line">    <span class="comment">// Footer 其实就是通知面板底部的两个按钮：“管理”、“全部清除”。</span></span><br><span class="line">    mNotificationStackScrollLayoutController.updateFooter();</span><br><span class="line">    <span class="comment">// 更新状态栏的通知图标</span></span><br><span class="line">    mNotificationIconAreaController.updateNotificationIcons(createVisibleEntriesList());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>至此通知面板的视图完成添加、更新。<br>下面接着看下状态栏的通知图标更新：<br><strong>NotificationIconAreaController#updateNotificationIcons()</strong><br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationIconAreaController.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updateNotificationIcons</span><span class="params">(List&lt;ListEntry&gt; entries)</span> &#123;</span><br><span class="line">    mNotificationEntries = entries;</span><br><span class="line">    updateNotificationIcons();</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">updateNotificationIcons</span><span class="params">()</span> &#123;</span><br><span class="line">    Trace.beginSection(<span class="string">&quot;NotificationIconAreaController.updateNotificationIcons&quot;</span>);</span><br><span class="line">        <span class="comment">// 更新状态栏图标</span></span><br><span class="line">    updateStatusBarIcons();</span><br><span class="line">    updateShelfIcons();</span><br><span class="line">    <span class="comment">// 更新 Aod 通知图标</span></span><br><span class="line">    updateAodNotificationIcons();</span><br><span class="line">    <span class="comment">// 应用通知图标色调</span></span><br><span class="line">    applyNotificationIconsTint();</span><br><span class="line">    Trace.endSection();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>下面都是调用 update XXX Icons() 这种类似的方法，接着调用 updateIconsForLayout() 方法，我们直接分析<br><strong>NotificationIconAreaController#updateIconsForLayout()：</strong><br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationIconAreaController.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">updateIconsForLayout</span><span class="params">(Function&lt;NotificationEntry, StatusBarIconView&gt; function,</span></span><br><span class="line"><span class="params">        NotificationIconContainer hostLayout, <span class="type">boolean</span> showAmbient, <span class="type">boolean</span> showLowPriority,</span></span><br><span class="line"><span class="params">        <span class="type">boolean</span> hideDismissed, <span class="type">boolean</span> hideRepliedMessages, <span class="type">boolean</span> hideCurrentMedia,</span></span><br><span class="line"><span class="params">        <span class="type">boolean</span> hideCenteredIcon)</span> &#123;</span><br><span class="line">    <span class="comment">// toShow保存即将显示的图标</span></span><br><span class="line">    ArrayList&lt;StatusBarIconView&gt; toShow = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;(</span><br><span class="line">            mNotificationScrollLayout.getChildCount());</span><br><span class="line">    <span class="comment">// 过滤通知，并保存需要显示的通知图标</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; mNotificationScrollLayout.getChildCount(); i++) &#123;</span><br><span class="line">        <span class="comment">// 获取一个通知视图</span></span><br><span class="line">        <span class="type">View</span> <span class="variable">view</span> <span class="operator">=</span> mNotificationScrollLayout.getChildAt(i);</span><br><span class="line">        <span class="keyword">if</span> (view <span class="keyword">instanceof</span> ExpandableNotificationRow) &#123;</span><br><span class="line">            <span class="type">NotificationEntry</span> <span class="variable">ent</span> <span class="operator">=</span> ((ExpandableNotificationRow) view).getEntry();</span><br><span class="line">            <span class="keyword">if</span> (shouldShowNotificationIcon(ent, showAmbient, showLowPriority, hideDismissed,</span><br><span class="line">                    hideRepliedMessages, hideCurrentMedia, hideCenteredIcon)) &#123;</span><br><span class="line">                <span class="comment">// 获取图标</span></span><br><span class="line">                <span class="type">StatusBarIconView</span> <span class="variable">iconView</span> <span class="operator">=</span> function.apply(ent);</span><br><span class="line">                <span class="keyword">if</span> (iconView != <span class="literal">null</span>) &#123;</span><br><span class="line">                    toShow.add(iconView);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="comment">// 把需要显示的图标添加到hostLayout中</span></span><br><span class="line">    <span class="keyword">final</span> FrameLayout.<span class="type">LayoutParams</span> <span class="variable">params</span> <span class="operator">=</span> generateIconLayoutParams();</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; toShow.size(); i++) &#123;</span><br><span class="line">        <span class="type">StatusBarIconView</span> <span class="variable">v</span> <span class="operator">=</span> toShow.get(i);</span><br><span class="line">        <span class="comment">// 如果刚刚删除并再次添加，视图可能仍会暂时添加</span></span><br><span class="line">        hostLayout.removeTransientView(v);</span><br><span class="line">        <span class="keyword">if</span> (v.getParent() == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (hideDismissed) &#123;</span><br><span class="line">                v.setOnDismissListener(mUpdateStatusBarIcons);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 执行到最后是 NotificationIconContainer.addView 添加视图</span></span><br><span class="line">            <span class="comment">// NotificationIconContainer本身没有addView、removeView方法，</span></span><br><span class="line">            <span class="comment">// 最终走的是其多层下去的父类ViewGroup的方法</span></span><br><span class="line">            hostLayout.addView(v, i, params);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
      <blockquote>
        
        <strong>本文链接：</strong><br><a href="http://longzhiye.top/2024/01/27/2024-01-27/">http://longzhiye.top/2024/01/27/2024-01-27/</a>
      </blockquote>
    </div>
    <footer class="mdui-card-actions">
      
        <a class="mdui-ripple article_categories-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a>
      
      
        <a class="mdui-ripple article_tags-link" href="/tags/Android/" rel="tag">Android</a><a class="mdui-ripple article_tags-link" href="/tags/framework/" rel="tag">framework</a>
      
    </footer>
    
  </article>
  
<script src="//cdn.bootcdn.net/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

  <script>$("#main article .mdui-card-content img.fancybox").on("click",function(e){$.fancybox.open({src:$(this).attr("src")});});</script>


  <nav id="paginator">
    
      <a rel="prev" class="extend prev" href="/2024/01/28/2024-01-28/">
        <button aria-label="prev" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_back</i></button>
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android14 StatusBar顶部图标加载流程'}">上一篇</span>
      </a>
    
    <div class="spacer"></div>
    
      <a rel="next" class="extend next" href="/2024/01/21/2024-01-21/">
        <span class="mdui-p-x-3" mdui-tooltip="{content: '应用启动的优化措施'}">下一篇</span>
        <button aria-label="next" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_forward</i></button>
      </a>
    
  </nav>





</main>
  <footer id="footer" class="mdui-m-t-5 mdui-p-y-3 mdui-color-theme">
  <div class="mdui-p-y-0 mdui-text-center">
    
    
    
    
    
    
    
      <a href="http://github.com/longzhiye" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe7ab;</i></a>
    
    
    
      <a href="https://www.zhihu.com/people/long-zhi-xie-61" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe6c0;</i></a>
    
    
    
      <a href="tencent://tencent:////message/?uin=951898105&Menu=yes" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe651;</i></a>
    
    
  </div>
  <div class="mdui-p-y-1 mdui-text-center">
    Copyright &copy; 2022 - 2025 龙之叶<br>
    Powered by <a href="https://hexo.io/" target="_blank" class="mdui-text-color-theme-accent">Hexo</a>
    <br>
      <span id="busuanzi_container_site_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_site_pv"></span></span> &nbsp;&nbsp;
      <span id="busuanzi_container_site_uv" style="display: none;"><i class="iconfont">&#xe601;</i> <span id="busuanzi_value_site_uv"></span></span>
    
  </div>
</footer>

  <button id="gotop" class="mdui-fab mdui-fab-fixed mdui-fab-hide mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_upward</i></button>
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
<script src="/js/mdui.js"></script>
<script src="/js/script.js"></script>

  
<script src="/custom.js"></script>

</body>
</html>
